#include "scale_mode_dialog.h"

#include <QButtonGroup>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>

#include "core/music_settings.h"

ScaleModeDialog::ScaleModeDialog(MusicSettings *settings, QWidget *parent):
  QDialog(parent),
  settings(settings) {
  setWindowTitle("Tonalité et mode");

  QButtonGroup *buttonGroup = new QButtonGroup(this);

  // non défini

  QGroupBox *undefinedModeBox = new QGroupBox("Non défini", this);
  QHBoxLayout *undefinedModeLayout = new QHBoxLayout;
  undefinedModeBox->setLayout(undefinedModeLayout);

  undefinedRadioButton = new QRadioButton("Pas de contrainte sur le mode "
                                          "et la tonalité", this);
  buttonGroup->addButton(undefinedRadioButton);
  undefinedModeLayout->addWidget(undefinedRadioButton);
  undefinedModeLayout->addStretch();

  // mode majeur

  QGroupBox *majorModeBox = new QGroupBox("Mode majeur", this);
  QGridLayout *majorModeLayout = new QGridLayout;
  majorModeBox->setLayout(majorModeLayout);

  //

  cM = new QRadioButton("Do", this);
  buttonGroup->addButton(cM);
  majorModeLayout->addWidget(cM, 0, 0);

  // dièses
  gM = new QRadioButton("Sol", this);
  buttonGroup->addButton(gM);
  majorModeLayout->addWidget(gM, 1, 0);

  dM = new QRadioButton("Ré", this);
  buttonGroup->addButton(dM);
  majorModeLayout->addWidget(dM, 1, 1);

  aM = new QRadioButton("La", this);
  buttonGroup->addButton(aM);
  majorModeLayout->addWidget(aM, 1, 2);

  eM = new QRadioButton("Mi", this);
  buttonGroup->addButton(eM);
  majorModeLayout->addWidget(eM, 1, 3);

  bM = new QRadioButton("Si", this);
  buttonGroup->addButton(bM);
  majorModeLayout->addWidget(bM, 1, 4);

  fisM = new QRadioButton("Fa dièse", this);
  buttonGroup->addButton(fisM);
  majorModeLayout->addWidget(fisM, 1, 5);

  cisM = new QRadioButton("Do dièse", this);
  buttonGroup->addButton(cisM);
  majorModeLayout->addWidget(cisM, 1, 6);

  // bémols

  fM = new QRadioButton("Fa", this);
  buttonGroup->addButton(fM);
  majorModeLayout->addWidget(fM, 2, 0);

  besM = new QRadioButton("Si bémol", this);
  buttonGroup->addButton(besM);
  majorModeLayout->addWidget(besM, 2, 1);

  eesM = new QRadioButton("Mi bémol", this);
  buttonGroup->addButton(eesM);
  majorModeLayout->addWidget(eesM, 2, 2);

  aesM = new QRadioButton("La bémol", this);
  buttonGroup->addButton(aesM);
  majorModeLayout->addWidget(aesM, 2, 3);

  desM = new QRadioButton("Ré bémol", this);
  buttonGroup->addButton(desM);
  majorModeLayout->addWidget(desM, 2, 4);

  gesM = new QRadioButton("Sol bémol", this);
  buttonGroup->addButton(gesM);
  majorModeLayout->addWidget(gesM, 2, 5);

  cesM = new QRadioButton("Do bémol", this);
  buttonGroup->addButton(cesM);
  majorModeLayout->addWidget(cesM, 2, 6);

  // mode mineur

  QGroupBox *minorModeBox = new QGroupBox("Mode mineur", this);
  QGridLayout *minorModeLayout = new QGridLayout;
  minorModeBox->setLayout(minorModeLayout);

  //
  am = new QRadioButton("La", this);
  buttonGroup->addButton(am);
  minorModeLayout->addWidget(am, 0, 0);

  // dièses

  em = new QRadioButton("Mi", this);
  buttonGroup->addButton(em);
  minorModeLayout->addWidget(em, 1, 0);

  bm = new QRadioButton("Si", this);
  buttonGroup->addButton(bm);
  minorModeLayout->addWidget(bm, 1, 1);

  fism = new QRadioButton("Fa dièse", this);
  buttonGroup->addButton(fism);
  minorModeLayout->addWidget(fism, 1, 2);

  cism = new QRadioButton("Do dièse", this);
  buttonGroup->addButton(cism);
  minorModeLayout->addWidget(cism, 1, 3);

  gism = new QRadioButton("Sol dièse", this);
  buttonGroup->addButton(gism);
  minorModeLayout->addWidget(gism, 1, 4);

  dism = new QRadioButton("Ré dièse", this);
  buttonGroup->addButton(dism);
  minorModeLayout->addWidget(dism, 1, 5);

  aism = new QRadioButton("La dièse", this);
  buttonGroup->addButton(aism);
  minorModeLayout->addWidget(aism, 1, 6);

  // bémols

  dm = new QRadioButton("Ré", this);
  buttonGroup->addButton(dm);
  minorModeLayout->addWidget(dm, 2, 0);

  gm = new QRadioButton("Sol", this);
  buttonGroup->addButton(gm);
  minorModeLayout->addWidget(gm, 2, 1);

  cm = new QRadioButton("Do", this);
  buttonGroup->addButton(cm);
  minorModeLayout->addWidget(cm, 2, 2);

  fm = new QRadioButton("Fa", this);
  buttonGroup->addButton(fm);
  minorModeLayout->addWidget(fm, 2, 3);

  besm = new QRadioButton("Si bémol", this);
  buttonGroup->addButton(besm);
  minorModeLayout->addWidget(besm, 2, 4);

  eesm = new QRadioButton("Mi bémol", this);
  buttonGroup->addButton(eesm);
  minorModeLayout->addWidget(eesm, 2, 5);

  aesm = new QRadioButton("La bémol", this);
  buttonGroup->addButton(aesm);
  minorModeLayout->addWidget(aesm, 2, 6);

  // boutons

  QPushButton *okButton = new QPushButton("&Valider", this);
  connect(okButton, SIGNAL(clicked()), this, SLOT(validate()));

  QPushButton *cancelButton = new QPushButton("&Annuler", this);
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));

  QHBoxLayout *buttonLayout = new QHBoxLayout;
  buttonLayout->addStretch();
  buttonLayout->addWidget(okButton);
  buttonLayout->addWidget(cancelButton);

  //

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->addWidget(undefinedModeBox);
  mainLayout->addWidget(majorModeBox);
  mainLayout->addWidget(minorModeBox);
  mainLayout->addLayout(buttonLayout);
  setLayout(mainLayout);

  initialize();
}

/*!
 * Initialise le bon bouton en fonction des paramètres
 */
void ScaleModeDialog::initialize() {
  switch(settings->mode) {
    case MusicSettings::UNDEFINED_MODE:
      undefinedRadioButton->setChecked(true);
      return;
    case MusicSettings::MAJOR:
      switch(settings->scale){
        case MusicSettings::C:
          cM->setChecked(true);
          return;

        case MusicSettings::G:
          gM->setChecked(true);
          return;
        case MusicSettings::D:
          dM->setChecked(true);
          return;
        case MusicSettings::A:
          aM->setChecked(true);
          return;
        case MusicSettings::E:
          eM->setChecked(true);
          return;
        case MusicSettings::B:
          bM->setChecked(true);
          return;
        case MusicSettings::FIS:
          fisM->setChecked(true);
          return;
        case MusicSettings::CIS:
          cisM->setChecked(true);
          return;

        case MusicSettings::F:
          fM->setChecked(true);
          return;
        case MusicSettings::BES:
          besM->setChecked(true);
          return;
        case MusicSettings::EES:
          eesM->setChecked(true);
          return;
        case MusicSettings::AES:
          aesM->setChecked(true);
          return;
        case MusicSettings::DES:
          desM->setChecked(true);
          return;
        case MusicSettings::GES:
          gesM->setChecked(true);
          return;
        case MusicSettings::CES:
          cesM->setChecked(true);
          return;

        default:
          return;
      }
    case MusicSettings::MINOR:
      switch(settings->scale) {
        case MusicSettings::A:
          am->setChecked(true);
          return;

        case MusicSettings::E:
          em->setChecked(true);
          return;
        case MusicSettings::B:
          bm->setChecked(true);
          return;
        case MusicSettings::FIS:
          fism->setChecked(true);
          return;
        case MusicSettings::CIS:
          cism->setChecked(true);
          return;
        case MusicSettings::GIS:
          gism->setChecked(true);
          return;
        case MusicSettings::DIS:
          dism->setChecked(true);
          return;
        case MusicSettings::AIS:
          aism->setChecked(true);
          return;

        case MusicSettings::D:
          dm->setChecked(true);
          return;
        case MusicSettings::G:
          gm->setChecked(true);
          return;
        case MusicSettings::C:
          cm->setChecked(true);
          return;
        case MusicSettings::F:
          fm->setChecked(true);
          return;
        case MusicSettings::BES:
          besm->setChecked(true);
          return;
        case MusicSettings::EES:
          eesm->setChecked(true);
          return;
        case MusicSettings::AES:
          aesm->setChecked(true);
          return;

        default:
          return;
      }
  }
}

/*!
 * Change les paramètres de tonalité et de mode en fonction
 * du bouton selectionné puis ferme la fenêtre
 */
void ScaleModeDialog::validate() {
  if(undefinedRadioButton->isChecked()) {
    settings->mode = MusicSettings::UNDEFINED_MODE;
    settings->scale = MusicSettings::UNDEFINED_SCALE;
    settings->firstNote = MusicSettings::UNDEFINED_DEGREE;
  }

  // mode majeur

  else if(cM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::C;
  }

  // dièses

  else if(gM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::G;
  }
  else if(dM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::D;
  }
  else if(aM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::A;
  }
  else if(eM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::E;
  }
  else if(bM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::B;
  }
  else if(fisM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::FIS;
  }
  else if(cisM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::CIS;
  }

  // bémols

  else if(fM->isChecked()){
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::F;
  }
  else if(besM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::BES;
  }
  else if(eesM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::EES;
  }
  else if(aesM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::AES;
  }
  else if(desM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::DES;
  }
  else if(gesM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::GES;
  }
  else if(cesM->isChecked()) {
    settings->mode = MusicSettings::MAJOR;
    settings->scale = MusicSettings::CES;
  }

  // mode mineur

  else if(am->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::A;
  }

  // dièses

  else if(em->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::E;
  }
  else if(bm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::B;
  }
  else if(fism->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::FIS;
  }
  else if(cism->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::CIS;
  }
  else if(gism->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::GIS;
  }
  else if(dism->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::DIS;
  }
  else if(aism->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::AIS;
  }

  // bémols

  else if(dm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::D;
  }
  else if(gm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::G;
  }
  else if(cm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::C;
  }
  else if(fm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::F;
  }
  else if(besm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::BES;
  }
  else if(eesm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::EES;
  }
  else if(aesm->isChecked()) {
    settings->mode = MusicSettings::MINOR;
    settings->scale = MusicSettings::AES;
  }

  close();
}
